import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { DeductionService } from './deduction.service';
import { UserSys } from 'src/user/userSys.entity';
import { CurrentUser } from 'src/user/user.decorator';
import { UserService } from 'src/user/user.service';
import { UserGuard } from 'src/role/role.guard';
import { RoleEnum } from 'src/role/role.enum';
import { ProjectService } from 'src/project/project.service';
import { NotFound } from 'src/util/http';
import { And, Between, FindOptionsWhere, In, LessThan, MoreThan } from 'typeorm';
import { Deduction } from './deduction.entity';
import * as moment from 'moment';
import { UseLog } from 'src/log/useLog.decorator';

@Controller('api/deduction')
@ApiTags("扣罚管理")
@UseGuards(AuthGuard("jwt"))
export class DeductionController {
    constructor(
        private readonly deduction: DeductionService,
        private readonly userService: UserService,
        private readonly projectService: ProjectService
    ) { }

    @Get("getmine")
    @ApiOperation({ summary: "获取我的记录" })
    getmine(
        @CurrentUser() user: UserSys
    ) {
        return this.deduction.deduction.find({
            where: {
                user: { id: user.id }
            },
            order: {
                id: "desc"
            },
            relations: ["target", "project"]
        })
    }
    @Get("getAll")
    getAll(
        @CurrentUser() user: UserSys
    ) {
        return this.deduction.deduction.find({
            where: {
                system: {
                    id: user.system.id
                }
            },
            relations: ["user"]
        })
    }

    @Get("get")
    @ApiOperation({ summary: "获取扣罚记录" })
    @UseGuards(UserGuard([RoleEnum.manager]))
    async get(
        @CurrentUser() user: UserSys,
        @Query("page") page: number = 1,
        @Query("limit") limit: number = 10,
        @Query("date") date: string,
        @Query("pid") project: number[]
    ) {
        const where: FindOptionsWhere<Deduction> = {
            system: {
                id: user.system.id
            }
        }
        if (user.role === RoleEnum.manager) {
            where.project = { user: { id: user.id } };
        }
        if (!!date) {
            const LastMonthLastDay = moment(date).subtract(1, "month").endOf("month").toDate();
            const nextMonthFirstDay = moment(date).startOf("month").add(1, "month").startOf("month").toDate()
            where.createAt = Between(LastMonthLastDay, nextMonthFirstDay)
        }
        if (!!project) where.project = { id: In(project) }
        const [data, count] = await this.deduction.deduction.findAndCount({
            order: {
                id: "desc"
            },
            where,
            relations: ["system", "target", "user", "project"],
            skip: (page - 1) * limit,
            take: limit
        })
        return { data, count }
    }

    @Post("create")
    @UseLog({ summary: "扣罚" })
    @UseGuards(UserGuard([RoleEnum.manager]))
    async create(
        @Body() { user, project, ...rest },
        @CurrentUser() target: UserSys
    ) {
        const system = { id: target.system.id }
        const users = await this.userService.userSys.find({
            where: { id: In(user), system }
        })
        await this.deduction.create(users, target, await this.projectService.project.findOne({ where: { id: project } }), rest)
    }

    @Post("del")
    @UseLog("删除扣罚记录")
    @UseGuards(UserGuard([RoleEnum.manager]))
    async del(
        @Body() { ids },
    ) {
        await this.deduction.deduction.delete({
            id: In(ids)
        })
    }
}
